package com.yincheng.oes.business.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Author: yangsy
 * @Contact: 446948434@qq.com
 * @Date: 2021/7/25 20:49
 * @Version: 1.0
 * @Description:
 */
public class DigitalUtils {

    private static final Logger logger = LogManager.getLogger(DigitalUtils.class);

    /**
     * 计算百分比
     *
     * @param score      score
     * @param totalScore totalScore
     * @return score/totalScore  * 100%
     */
    public static String calProportionPercentage(Double score, Double totalScore) {
        if (Double.isNaN(score) || Double.isNaN(totalScore)) {
            logger.error("score is {}, totalScore is {}", score, totalScore);
            return "0";
        }
        BigDecimal bTotalScore = new BigDecimal(totalScore);
        BigDecimal bScore = BigDecimal.valueOf(score);
        String scorePercentage = getPercentage(bTotalScore, bScore);
        return scorePercentage;
    }

    private static String getPercentage(BigDecimal bTotalScore, BigDecimal bScore) {
        String scorePercentage;
        try {
            scorePercentage = bScore.divide(bTotalScore, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))
                    .toString();
        } catch (Exception e) {
            logger.error("calProportionPercentage error, score is {}, totalScore is {}", bScore, bTotalScore, e);
            scorePercentage = "0";
        }
        return scorePercentage;
    }

    /**
     * 计算增长率
     *
     * @param score     score
     * @param nextScore nextScore
     * @return (nextScore - score)/score * 100%
     */
    public static String calIncreasePercentage(String score, String nextScore) {
        if (StringUtils.isEmpty(score) || StringUtils.isEmpty(nextScore)) {
            logger.error("score is {}, nextScore is {}", score, nextScore);
            return "0";
        }
        BigDecimal b1 = new BigDecimal(score);
        BigDecimal b2 = new BigDecimal(nextScore);
        BigDecimal temp1 = b2.subtract(b1);
        String scorePercentage;
        try {
            scorePercentage = temp1.divide(b1, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).toString();
        } catch (Exception e) {
            logger.error("calIncreasePercentage error, score is {}, nextScore is {}", score, nextScore, e);
            scorePercentage = "0";
        }
        return scorePercentage;
    }

    public static String calProportionPercentage(Float score, Float totalScore) {
        if (Double.isNaN(score) || Double.isNaN(totalScore)) {
            logger.error("score is {}, totalScore is {}", score, totalScore);
            return "0";
        }
        BigDecimal bTotalScore = new BigDecimal(totalScore);
        BigDecimal bScore = BigDecimal.valueOf(score);
        String scorePercentage = getPercentage(bTotalScore, bScore);
        return scorePercentage;
    }

    public static Integer format(Double a) {
        if (a == null) {
            return 0;
        }
        String format = new DecimalFormat("0").format(a);
        return Integer.valueOf(format);
    }

    public static Integer format(Float a) {
        if (a == null) {
            return 0;
        }
        String format = new DecimalFormat("0").format(a);
        return Integer.valueOf(format);
    }

}
